import java.io.*;
import java.util.ArrayList;

/**
 * @author gaopeng
 * @date 2019/12/9 10:07 下午
 */
public class Deep2 implements Serializable {
    /**
     * final类型不能clone
     */
    private ArrayList<String> list = new ArrayList<>(2);

    public ArrayList<String> getList() {
        return list;
    }

    public void setList(ArrayList<String> list) {
        this.list = list;
    }

    public Deep2 streamClone(){
        ByteArrayOutputStream bo=null;
        ObjectOutputStream oo=null;
        ByteArrayInputStream bi=null;
        ObjectInputStream oi=null;
        Deep2 deep2=null;
        try {
            bo=new ByteArrayOutputStream();
            oo=new ObjectOutputStream(bo);
            oo.writeObject(this);
            bi=new ByteArrayInputStream(bo.toByteArray());
            oi=new ObjectInputStream(bi);
            deep2= (Deep2) oi.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(bo!=null){
                    bo.close();
                }
                if(oo!=null){
                    oo.close();
                }
                if(bi!=null){
                    bi.close();
                }
                if(oi!=null){
                    oi.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        return deep2;
    }

    public static void main(String[] args) {
        Deep2 deep2 = new Deep2();
        deep2.getList().add("zhangsan");
        Deep2 clone = deep2.streamClone();
        clone.getList().add("lisi");
        deep2.getList().forEach(System.out::println);
    }
}
